#!/bin/bash

# Nginx日志生成器状态检查脚本

echo "=== Nginx日志生成器状态检查 ==="

# 检查Java进程
PID=$(ps aux | grep java | grep nginx-log-generator | grep -v grep | awk '{print $2}')

if [ -z "$PID" ]; then
    echo "状态: ❌ 未运行"
else
    echo "状态: ✅ 运行中"
    echo "进程PID: $PID"
    
    # 获取进程详细信息
    if command -v ps > /dev/null; then
        echo "进程详情:"
        ps -p $PID -o pid,ppid,cmd,etime,%mem,%cpu --no-headers
    fi
    
    # 检查日志文件
    if [ -f "access.log" ]; then
        echo "主日志文件:"
        ls -lh access.log
        echo "最近更新时间: $(stat -c %y access.log 2>/dev/null || stat -f %Sm access.log 2>/dev/null || echo '无法获取')"
    fi
    
    # 检查轮转日志目录
    if [ -d "logs" ]; then
        echo "轮转日志目录:"
        ls -lh logs/ | tail -5
    fi
fi

# 显示系统资源使用情况
echo ""
echo "系统资源:"
if command -v df > /dev/null; then
    echo "磁盘空间:"
    df -h . | tail -1
fi

if command -v free > /dev/null; then
    echo "内存使用:"
    free -h | grep Mem
elif [ -f /proc/meminfo ]; then
    echo "内存使用:"
    grep MemAvailable /proc/meminfo || grep MemFree /proc/meminfo
fi

echo ""
echo "状态检查完成！"